package com.lczyfz.istep.modules.openstack.api.neutron.impl;

import com.lczyfz.istep.modules.openstack.api.neutron.impl.parseJson.NeutronParse;
import com.lczyfz.istep.modules.openstack.api.neutron.interfaces.Subnets;
import com.lczyfz.istep.modules.openstack.api.neutron.model.Subnet;
import com.lczyfz.istep.modules.openstack.constvary.ConstParams;
import com.lczyfz.istep.modules.openstack.constvary.RequestType;
import com.lczyfz.istep.modules.openstack.utils.HTTP;
import com.lczyfz.istep.modules.openstack.utils.Request;
import com.lczyfz.istep.modules.openstack.utils.RequestFactory;
import com.lczyfz.istep.modules.openstack.utils.Response;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.List;

/**
 * �����ӿڵ�ʵ����
 * @author clq
 *
 */
public class SubnetImpl implements Subnets {
	public RequestFactory factory = new RequestFactory("neutron");

	@Override
	public List<Subnet> getSubnets() {
		List<Subnet> subnets = null;
		Request request = factory.getRequest();
		request.setUrl("/v2.0/subnets");
		request.setRequestType(RequestType.GET);
		
		Response results = HTTP.getConnectionResult(request);
		JSONObject body = results.getResponseBody();
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}	
		
		subnets = NeutronParse.resolveSubnets(body);
		return subnets;
	}

	@Override
	public Subnet getSubnetDetail(String subnetId) {
		Subnet subnet = new Subnet();
		Request request = factory.getRequest();
		request.setUrl("/v2.0/subnets/" + subnetId);
		request.setRequestType(RequestType.GET);
		
		Response results = HTTP.getConnectionResult(request);
		JSONObject body = results.getResponseBody();
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		
		subnet = NeutronParse.resolveSubnetDetail(body);
		return subnet;
	}

	@Override
	public Subnet createSubnet(Subnet subnetinput) {
		Subnet subnet = new Subnet();
		JSONObject params = NeutronParse.packageSubnet(subnetinput);
	//	System.out.println(params.toString());
		Request request = factory.getRequest();
		request.setUrl("/v2.0/subnets");
		request.setRequestType(RequestType.POST);
		request.setBody(params);
		
		Response results = HTTP.getConnectionResult(request);
		if( ConstParams.RESPONSE_CODE_CREATED!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		
		subnet = NeutronParse.resolveSubnetDetail(res);
		updateAllocationPools(subnet);
		return subnet;
	}
	private void updateAllocationPools(Subnet subnet) {
		Request request = factory.getRequest();
		request.setUrl("/v2.0/subnets/" + subnet.getsubnetId());
		request.setRequestType(RequestType.PUT);

		String preIp = getNetworkNumber(subnet.getsubnetCidr());
		JSONObject params = new JSONObject();
		JSONObject sub = new JSONObject();
		JSONArray allocationPools = new JSONArray();
		JSONObject firstPools = new JSONObject();
		JSONObject SecondPools = new JSONObject();
		firstPools.put("start", preIp + ".252");
		firstPools.put("end", preIp + ".254");
		SecondPools.put("start", preIp + ".1");
		SecondPools.put("end", preIp + ".249");
		allocationPools.add(firstPools);
		allocationPools.add(SecondPools);
		sub.put("allocation_pools",allocationPools);
		params.put("subnet",sub);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);
		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
	}
	@Override
	public void deleteSubnet(String subnetId) {
		Request request = factory.getRequest();
		request.setUrl("/v2.0/subnets/" + subnetId);
		request.setRequestType(RequestType.DELETE);
		
		Response results = HTTP.getConnectionResult(request);
		if(ConstParams.RESPONSE_CODE_SUCCESS  != results.getResponseCode() )
		{
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		return;
		
	}

	@Override
	public Subnet updateSubnet(String subnetId , Subnet subnetupdateinput) {
		Subnet subnet = new Subnet();
		JSONObject params = NeutronParse.packageSubnet(subnetupdateinput);
	//	System.out.println(params.toString());
		System.out.println("/v2.0/subnets/" + subnetId);
		Request request = factory.getRequest();
		request.setUrl("/v2.0/subnets/" + subnetId);
		request.setRequestType(RequestType.PUT);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);	
		
		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		
		subnet = NeutronParse.resolveUpdateSubnet(res);
		return subnet;
	}
	
	public boolean checkUniqueName(String subnetName){
		return true;
	}
	
	public static void main(String[] args){
		SubnetImpl subnettest = new SubnetImpl();
		System.out.println("������Ϣ");
		List<Subnet> subnetlist = subnettest.getSubnets();
		for(Subnet sn : subnetlist){
			System.out.println(sn.getsubnetId()+" "+sn.getsubnetName() + " "+sn.getsubnetNetworkId()+" "+sn.getsubnetCidr()+
					" "+sn.getsubnetGatewayIp()+" "+sn.getsubnetIpVersion() + " "+sn.getsubnetProjectId() + " "+sn.getsubnetTenantId()+
					" "+sn.getsubnetUpdateAt()+ " "+sn.getsubnetCreateAt());
		}
		
//		System.out.println("������ϸ��Ϣ");
//		Subnet sn = subnettest.getSubnetDetail("354e98c2-3849-422d-9b4a-e6164532e92e");
//		System.out.println(sn.getsubnetId()+" "+sn.getsubnetName() + " "+sn.getsubnetNetworkId()+" "+sn.getsubnetCidr()+
//				" "+sn.getsubnetGatewayIp()+" "+sn.getsubnetIpVersion() + " "+sn.getsubnetProjectId() + " "+sn.getsubnetTenantId()+
//				" "+sn.getsubnetUpdateAt()+ " "+sn.getsubnetCreateAt());
		
		System.out.println("��������");
		Subnet sntt = new Subnet();
		sntt.setsubnetName("网络一");
		sntt.setsubnetNetworkId("a5c9f11d-9d33-4369-a9d5-ec2be72b11f0");
		sntt.setsubnetIpVersion("4");
		sntt.setsubnetCidr("10.0.0.0/24");
		Subnet sn_new = subnettest.createSubnet(sntt);
		//Subnet sn_new = subnettest.createSubnet("snet","58624e8e-5243-485d-b9dd-eb96fbaddc9c", "4" , "9f3156cf-b734-46d8-91ad-84a787fd41dc");
		System.out.println(sn_new.getsubnetId()+" "+sn_new.getsubnetName() + " "+sn_new.getsubnetNetworkId()+" "+sn_new.getsubnetCidr()+
				" "+sn_new.getsubnetGatewayIp()+" "+sn_new.getsubnetIpVersion() + " "+sn_new.getsubnetProjectId() + " "+sn_new.getsubnetTenantId()+
				" "+sn_new.getsubnetUpdateAt()+ " "+sn_new.getsubnetCreateAt());

	//	System.out.println("ɾ������");
	//	subnettest.deleteSubnet("b10df017-c2da-49fc-99cd-544bf12cc015");
		
	/*	System.out.println("��������");
		Subnet subnettt = new Subnet();
		subnettt.setsubnetName("snet33");
		Subnet subnet_update = subnettest.updateSubnet("b10df017-c2da-49fc-99cd-544bf12cc015" ,subnettt);
		System.out.println(subnet_update.getsubnetId()+" "+subnet_update.getsubnetName() + " "+subnet_update.getsubnetNetworkId()+" "+subnet_update.getsubnetCidr()+
				" "+subnet_update.getsubnetGatewayIp()+" "+subnet_update.getsubnetIpVersion() + " "+subnet_update.getsubnetProjectId() + " "+subnet_update.getsubnetTenantId());
	*/
	}
	public static String getNetworkNumber(String ipAddress) {
		String[] networkNumber = ipAddress.split("\\.");
		if (networkNumber.length >= 3) {
			return networkNumber[0] + "." + networkNumber[1] + "." + networkNumber[2];
		}
		return null;
	}
}
